<?php
namespace app\admin\controller;
use \think\Controller;
use \think\captcha\Captcha;
use \think\Db;
use \think\Session;
use app\admin\model\Login;
class index extends Controller
{
      public function index(){

      if(request()->isPost()){
            $ca=input('verifycode');
            /*echo $ca;*/
            if(!captcha_check($ca)){
                //验证失败
                echo"<script>alert('验证码错误');</script>";
            }else{
                  //判断用户名密码
                  $a=input('a_name');
                  $p=input('a_password');
                  $log=new Login;
                  $result=$log->login($a,$p);
                  if ($result==1) {
                        echo"<script>alert('用户不存在');</script>";
                  }else if($result==2){
                        echo"<script>alert('密码错误');</script>";

                  }else if($result==3){
                    $this->redirect("project/index");
                  }
                  //错误分情况提醒，用户不存在、密码错误
            }

      }
        return $this->fetch();
      }
    public function show_captcha(){
        ob_clean();
        $captcha = new Captcha();
        $captcha->fontSize = 32;
        $captcha->length   = 3;
        $captcha->useNoise = false;
        return $captcha->entry();
    }
    public function logout(){
      Session::clear();
      $this->redirect('index/index');
    }

    public function check(){
    $u=input('user');
    $count=Db::name("admins")->where("a_name",$u)->count();
    echo $count;
    }


    /*public function edit(){

    $admin=Db::name("admins")->find();
    $this->assign("admin",$admin);
    return $this->fetch();

    $a_id=input('a_id');
    $admin=Db::name("admins")->find($a_id);
    echo json_encode($admin);
      }

    public function update(){
      $data=[
        'a_name'=>input('param.a_name'),
       'a_password'=>md5(input('param.a_password')),
      ];
      var_dump($data);
//修改数表
      $a_id=input('a_id');
      if(Db::name('admins')->where("a_id",$a_id)->update($data)){
        $this->success('修改成功');
      }else{
        $this->error('修改失败');
      }
    }*/
  

}
